<?php

require_once 'db/Result.php';
require_once 'db/StudentService.php';
require_once 'db/User.php';

/**
 * 学生管理
 */
class StudentManage
{

    public function listStudentInfoByPage(): Result
    {
        $jsonStr = file_get_contents('php://input');
        $request = json_decode($jsonStr);
        $stu = Student::empty();
        $stu->id = $request->id;
        $stu->name = $request->name;
        $studentService = new StudentService();
        // 从get请求参数中获取到分页相关参数
        $cur = $_GET['cur'];
        $pageSize = $_GET['pageSize'];
        return $studentService->listStudentInfoByPage($stu, new Page($cur, $pageSize));
    }

    public function addStudent(): void
    {
        $studentService = new StudentService();
        $studentService->insert($this->getStuFromJson());
    }

    public function getStuFromJson(): Student
    {
        $jsonStr = file_get_contents('php://input');
        $request = json_decode($jsonStr);
        $stu = Student::empty();
        $stu->id = $request->id;
        $stu->name = $request->name;
        $stu->sex = $request->sex;
        $stu->age = $request->age;
        $stu->city = $request->city;
        return $stu;
    }

    public function delById(): void
    {
        $id = $_GET['id'];
        $studentService = new StudentService();
        $studentService->delById($id);

    }

    public function getById(): Student
    {
        $id = $_GET['id'];
        $studentService = new StudentService();
        return $studentService->getById($id);
    }

    public function updateInfo(): void
    {
        $curInfo = $this->getStuFromJson();
        $studentService = new StudentService();
        // 先删除旧数据，再新增新数据
        $studentService->delById($curInfo->id);
        $studentService->insert($curInfo);
    }

    public function login(): Result
    {
        $curUser = $this->getUserFromJson();
        if (is_null($curUser->username) || empty(trim($curUser->username))) {
            return Result::error("请填写用户名");
        }
        if (is_null($curUser->password) || empty(trim($curUser->password))) {
            return Result::error("请填写密码");
        }
        $studentService = new StudentService();
        $oldUserInfo = $studentService->getByUsername($curUser->username);
        if (is_null($oldUserInfo) || !password_verify($curUser->password, $oldUserInfo->password)) {
            return Result::error("用户名或密码错误");
        }
        // 登录成功后向session会话中加入数据
        if (!is_null($oldUserInfo)) {
            session_start();
            $_SESSION['userid'] = $oldUserInfo->userid;
            $_SESSION['username'] = $oldUserInfo->username;
        }
        return Result::success($curUser);
    }

    public function register(): Result
    {
        $curUser = $this->getUserFromJson();
        if (empty(trim($curUser->username))) {
            return Result::error("请填写用户名");
        }
        if (empty(trim($curUser->password))) {
            return Result::error("请填写密码");
        }
        $studentService = new StudentService();
        $oldUserInfo = $studentService->getByUsername($curUser->username);
        if (!is_null($oldUserInfo)) {
            return Result::error("用户名已存在");
        }

        $oldUserInfo = $studentService->getByEmail($curUser->email);
        if (!is_null($oldUserInfo)) {
            return Result::error("邮箱已存在");
        }
        $studentService->insertUser($curUser);
        return Result::empty();
    }

    public function logout(): void
    {
        session_start();
        if (isset($_SESSION["userid"])) {
            unset($_SESSION['userid']);
            unset($_SESSION['username']);
        }
    }

    public function getUserFromJson(): User
    {
        $jsonStr = file_get_contents('php://input');
        $request = json_decode($jsonStr);
        $user = User::empty();
        if (property_exists($request, 'userid')) {
            $user->userid = $request->userid;
        }
        if (property_exists($request, 'username')) {
            $user->username = $request->username;
        }
        if (property_exists($request, 'password')) {
            $user->password = $request->password;
        }

        if (property_exists($request, 'email')) {
            $user->email = $request->email;
        }
        return $user;
    }

}